import vue from "@vitejs/plugin-vue";
import { defineConfig } from "vite";
import pxToRem from "postcss-pxtorem";

import path from "path";
import { fileURLToPath } from "url";

// 获取当前目录名
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// 前端代码定位插件
import { codeInspectorPlugin } from "code-inspector-plugin";

// element-plus 按需加载，main.ts 中无需引入样式文件，只需 app.use(ElementPlus)
import AutoImport from "unplugin-auto-import/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import Components from "unplugin-vue-components/vite";

// https://vite.dev/config/
export default defineConfig({
  envDir: "env",

  base: "./",

  plugins: [
    vue(),

    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),

    Components({
      resolvers: [ElementPlusResolver()],
    }),

    codeInspectorPlugin({
      bundler: "vite",
    }),
  ],

  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },

  server: {
    port: 8888,

    proxy: {
      "/API/api": {
        target: "http://127.0.0.1:8080", // 后端服务的地址
        changeOrigin: true, // 是否更改请求头中的 Origin 字段
        rewrite: path => path.replace(/^\/API\/api/, "/api"), // 将请求路径从 /api 重写为 /API/api
      },
    },
  },

  preview: {
    port: 9999,

    proxy: {
      "/api": {
        target: "http://127.0.0.1:8080", // 后端服务的地址
        changeOrigin: true, // 是否更改请求头中的 Origin 字段
        rewrite: path => path.replace(/^\/api/, "/api"), // 将请求路径从 /api 重写为 /api
      },
    },
  },
});
